package com.lungle.warmdiary.app.view.active.adapter;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.response.CampaignComment;
import com.lungle.warmdiary.app.model.response.Comment;
import com.lungle.warmdiary.app.ui.widget.AutoGridView;
import com.lungle.warmdiary.app.view.testcampaign.CampaignDetailListTab;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.iwf.photopicker.PhotoPreview;


public class ActiveCommentAdapter extends ListBaseAdapter {

    //    private ImagePreview mImagePreview;
    private Activity mActivity;
    private Fragment fragment;
    private static final int CONTENT_MAX = 200;

    public void setActivity(Activity activity) {
        this.mActivity = activity;
    }

    public void setFragment(Fragment fragment) {
        this.fragment = fragment;
    }


    @Override
    protected View getRealView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_active_comment, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }


        if (mDatas.get(position) instanceof Comment) {
            Comment comment = (Comment) mDatas.get(position);
            vh.rateItemName.setText(comment.getUser());
            vh.rateItemLevel.setVisibility(View.GONE);

//        vh.mTvCommentTime.setText("亲测时间: " + comment.getTime());

            if (Integer.parseInt(comment.getRate()) == 0) {
                vh.rateItemRatingbar.setVisibility(View.GONE);
                vh.rateItemRatingtext.setVisibility(View.GONE);
            } else {
                vh.rateItemRatingbar.setVisibility(View.VISIBLE);
                vh.rateItemRatingtext.setVisibility(View.VISIBLE);
            }

            vh.rateItemRatingbar.setRating(Integer.parseInt(comment.getRate()));
            vh.rateItemRatingtext.setText(CampaignDetailListTab.getTitleByRate(Integer.parseInt(comment.getRate())));
            String content = comment.getContent();
            if (content.length() > CONTENT_MAX) {
                content = content.substring(0, CONTENT_MAX);
                vh.rateItemContent.setText(String.format("%s...", content));
            } else {
                vh.rateItemContent.setText(content);
            }

            if (comment.getImages().size() > 0) {
                vh.rateItemImages.setVisibility(View.VISIBLE);
                vh.rateItemImages.setAdapter(new ActiveImageAdapter(parent.getContext(), comment.getImages(), 5));

                vh.rateItemImages.setOnItemClickListener((parent1, view, position1, id) -> {
//                    if (mImagePreview == null && mActivity != null) {
//                        mImagePreview = new ImagePreview(mActivity);
//                    }
//                    if (mImagePreview != null)
//                        mImagePreview.showPreview(view, comment.getImages().get(position1));

                    if (fragment != null) {
                        PhotoPreview.builder()
                                .setPhotos((ArrayList) comment.getImages())
                                .setCurrentItem(position)
                                .setShowDeleteButton(false)
                                .start(fragment.getActivity().getBaseContext(), fragment,0);
                    }
                });
            }
        } else if (mDatas.get(position) instanceof CampaignComment) {
            CampaignComment comment = (CampaignComment) mDatas.get(position);

            vh.rateItemName.setText(comment.getUser());
            if (comment.getRate() == 0) {
                vh.rateItemRatingbar.setVisibility(View.GONE);
                vh.rateItemRatingtext.setVisibility(View.GONE);
            } else {
                vh.rateItemRatingbar.setVisibility(View.VISIBLE);
                vh.rateItemRatingtext.setVisibility(View.VISIBLE);
            }

            vh.rateItemLevel.setVisibility(View.VISIBLE);

            if (comment.getTester() == 1)
                vh.rateItemLevel.setText("(亲测师)");
            else
                vh.rateItemLevel.setText(String.format("(%s)", comment.getLevel()));



            vh.rateItemRatingbar.setRating(comment.getRate());
            vh.rateItemRatingtext.setText(CampaignDetailListTab.getTitleByRate(comment.getRate()));
            String content = comment.getContent();
            if (content.length() > CONTENT_MAX) {
                content = content.substring(0, CONTENT_MAX);
                vh.rateItemContent.setText(String.format("%s...", content));
            } else {
                vh.rateItemContent.setText(content);
            }

            if (comment.getImages().size() > 0) {
                vh.rateItemImages.setVisibility(View.VISIBLE);
                vh.rateItemImages.setAdapter(new ActiveImageAdapter(parent.getContext(), comment.getImages(), 5));

                vh.rateItemImages.setOnItemClickListener((parent1, view, position1, id) -> {

                    if (fragment != null) {
                        PhotoPreview.builder()
                                .setPhotos((ArrayList) comment.getImages())
                                .setCurrentItem(position)
                                .setShowDeleteButton(false)
                                .start(fragment.getActivity().getBaseContext(), fragment,0);
                    }
//                    if (mImagePreview == null && mActivity != null) {
//                        mImagePreview = new ImagePreview(mActivity);
//                    }
//                    if (mImagePreview != null)
//                        mImagePreview.showPreview(view, comment.getImages().get(position1));

                });
            }

        }


        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.rate_item_name)
        TextView rateItemName;
        @Bind(R.id.rate_item_level)
        TextView rateItemLevel;
        @Bind(R.id.rate_item_ratingbar)
        RatingBar rateItemRatingbar;
        @Bind(R.id.rate_item_ratingtext)
        TextView rateItemRatingtext;
        @Bind(R.id.rate_item_content)
        TextView rateItemContent;
        @Bind(R.id.rate_item_images)
        AutoGridView rateItemImages;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
